package com.qingxin.park.action;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.park.entity.Manufacturer;
import com.qingxin.park.service.ManufacturerService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.entity.SysGroup;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.SysGroupService;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.utils.StringUtils;

@Controller
@RequestMapping("/manufacturer")
public class ManufacturerAction extends BaseAction {

	@Autowired
	private ManufacturerService manufacturerService;
	
	@Autowired
	private SysGroupService sysGroupService;
	
	
	
	@RequestMapping(value="/getManufacturerData.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getManufacturerData(String groupid, HttpServletRequest request){
		if(StringUtils.isNotEmpty(groupid)){
			SysGroup group = sysGroupService.get(groupid);
			if(group.getGrouptype() == 3){
				groupid = group.getPid();
			}
		}
		return manufacturerService.getManufacturerData(groupid, getLoginUser(request));
	}
	
	
	
	
	/**
	 * 加载左侧
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/lefttree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public List<JsTreeNode> lefttree(String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		if(StringUtils.isEmpty(pid)){
			JsTreeNode root = new JsTreeNode("0", "所有厂商","MANUFACTURER", null,true);
			list.add(root);
		}else{
			list.addAll(manufacturerService.getUITreeData(getLoginUser(request)));
		}
		return list;
	}
	
	/**
	 * 列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "trace/manufacturer/manufacturer_list";
	}
	
	/**
	 * 列表数据
	 * @param search
	 * @param user_type
	 * @param user_status
	 * @param regionid
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String datatype, String leftdata, String search,String manufacturerid, PageParam pageParam, HttpServletRequest request){
		String groupid = "";
		if("GROUP".equalsIgnoreCase(datatype) && StringUtils.isNotEmpty(leftdata) && !"0".equals(leftdata)){
			groupid = leftdata;
		}
		
		return manufacturerService.list(groupid, search,manufacturerid, pageParam, getLoginUser(request));
	}
	
	
	/**
	 * 添加企业信息
	 * @param model
	 * @param leftdata
	 * @param datatype
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do" , method=RequestMethod.GET)
	public String add(ModelMap model, String datatype, String leftdata, HttpServletRequest request){
		if("GROUP".equalsIgnoreCase(datatype) && StringUtils.isNotEmpty(leftdata) && !"0".equals(leftdata)){
			model.put("groupid", leftdata);
			
			SysGroup group = sysGroupService.get(leftdata);
			if(group.getGrouptype() != 4){
				model.put("errormessage", "该分组【"+group.getGroupname()+"】不是一个企业类型分组，不能在该分组下创建农资厂商信息");
				return "sys/error/error";
			}
			
			Manufacturer manufacturer = manufacturerService.getByProperty("groupid", leftdata);
			if(manufacturer != null){
				model.put("errormessage", "该分组【"+group.getGroupname()+"】已存在农资厂商信息，不能在该分组下重复创建企业信息");
				return "sys/error/error";
			}
			
			
		}else{
			model.put("groupid", "");
		}
		return "trace/manufacturer/manufacturer_add";
	}
	
	
	/**
	 * 保存企业信息
	 * @param company
	 * @param result
	 * @param typeid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST})
	@ResponseBody
	public Object save(@Valid Manufacturer manufacturer, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		//System.out.println(company.getGroupid());
		String manufacturerMax = manufacturerService.getManufacturerNoMax();
		if(StringUtils.isNotEmpty(manufacturerMax)){
			int number = Integer.parseInt(manufacturerMax)+1;
			String maxNo = number+"";
			if(maxNo.length()==1){
				maxNo = "00"+maxNo;
			}else if(maxNo.length()==2){
				maxNo = "0"+maxNo;
			}
			manufacturer.setManufacturerNo(maxNo);
		}else{
			manufacturer.setManufacturerNo("001");
		}
		
		SysError info = manufacturerService.add(manufacturer, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 修改企业信息
	 * @param model
	 * @param companyid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do" , method=RequestMethod.GET)
	public String edit(ModelMap model, String manufacturerid, HttpServletRequest request){
		Manufacturer manufacturer = manufacturerService.get(manufacturerid);
		model.put("manufacturer", manufacturer);
		return "trace/manufacturer/manufacturer_edit";
	}
	
	
	/**
	 * 保存修改的信息
	 * @param company
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method={RequestMethod.POST})
	@ResponseBody
	public Object update(@Valid Manufacturer manufacturer, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		SysError info = manufacturerService.update(manufacturer, getLoginUser(request), request);
		return info;
	}
	
	
	
	/**
	 * 删除企业信息
	 * @param materialid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method={RequestMethod.POST})
	@ResponseBody
	public Object delete(String[] manufacturerid, HttpServletRequest request){
		if(manufacturerid==null || manufacturerid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return manufacturerService.delete(manufacturerid, getLoginUser(request), request);
	}
	
	
	/**
	 * 判断企业名称是否存在
	 * @param blockName
	 * @param blockid
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNameDuplicate(String manufacturerName, String manufacturerid){
		boolean existed = manufacturerService.isNameDuplicate(manufacturerName, manufacturerid);
		return existed ? "1":"0";
	}
	
	
	/**
	 * 判断企业编号是否存在
	 * @param blockCode
	 * @param blockid
	 * @param parkid
	 * @return
	 */
	@RequestMapping(value="/isNoDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isCodeDuplicate(String manufacturerNo, String manufacturerid){
		boolean existed = manufacturerService.isCodeDuplicate(manufacturerNo, manufacturerid);
		return existed ? "1":"0";
	}
	
	
	/**
	 * 设置坐标点
	 * @param model
	 * @param type 坐标点类型
	 * @param id 坐标点ID，可能为空
	 * @param lng
	 * @param lat
	 * @param view 是否查看模式
	 * @return
	 */
	@RequestMapping(value="/setMapManufacturer.do", method=RequestMethod.GET)
	public String setparkmap(ModelMap model, String id, String view){
		model.put("id", id==null ? "" : id);
		model.put("view", view==null ? false : view);
		
		if(StringUtils.isNotEmpty(id)){
			Manufacturer manufacturer = manufacturerService.get(id);
			/*if(company != null){
				 company = new Company(company.getCompanyid(),company.getCompanyName(),company.getLat(), company.getLng());*/
				model.put("manufacturer", manufacturer);
			
		}
		return "trace/manufacturer/setMapManufacturer";
	}
	

}
